package com.zoe.onelink.core.check;

import com.zoe.onelink.plugin.check.AbstractEnvironmentChecker;
import com.zoe.onelink.plugin.check.model.PropertyCheckResult;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-02-22
 */
public class JVMEnvironmentChecker extends AbstractEnvironmentChecker {
    @Override
    protected List<PropertyCheckResult<?>> doCheck() {
        List<PropertyCheckResult<?>> propertyCheckResults = new ArrayList<>();
        MemoryUsage heapUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        // 堆内存小于等于1024MB
        long heapMaxUsage = heapUsage.getMax() / 1024 / 1024;
        if (heapMaxUsage <= 1024) {
            PropertyCheckResult<Object> checkResult = new PropertyCheckResult<>()
                    .setPropKey("-Xmx")
                    .setPropValue(heapMaxUsage)
                    .setLevel(PropertyCheckResult.Level.DANGER)
                    .setMessage("最大堆内存小于1024MB,建议调整到1024MB以上");
            propertyCheckResults.add(checkResult);
        }
        return propertyCheckResults;
    }
}
